//
// Created by lester on 2023/12/21.
//

#ifndef TEST_LENGTHBUFFER_H
#define TEST_LENGTHBUFFER_H
template<size_t SIZE>
class LengthBuffer
{
public:
    LengthBuffer(){
        memZero();
        cur_ = data_array_;
    }
    void Append(const void* data,size_t size);
    void Append(const char* data,size_t size);
    const char* begin() const{
        return data_array_;
    }
    char* begin() {
        return data_array_;
    }

    const char* end() const{
        return &data_array_[SIZE];
    }

    char* end(){
        return &data_array_[SIZE];
    }

    size_t length() const {
        return end() - begin();
    }

    size_t avail(){
        return end() - cur_;
    }

    void memZero(){
        memset(begin(),0,end()-begin());
    }

    void Reset(){
        cur_ = data_array_;
        memZero();
    }

    const char* Cur() const
    {
        return cur_;
    }

    char* Cur(){
        return cur_;
    }
private:
    char data_array_[SIZE];
    char* cur_;
};

template<size_t SIZE>
void LengthBuffer<SIZE>::Append(const void *data, size_t size) {
    Append((const char*)data,size);
}

template<size_t SIZE>
void LengthBuffer<SIZE>::Append(const char *data, size_t size) {
    if(avail() >= size){
        memcpy(Cur(),data,size);
        cur_ += size;
    }
}
using SmallBuffer   =  LengthBuffer<1024>;
using MiddleBuffer  =  LengthBuffer<2048>;
using LargeBuffer   =  LengthBuffer<4096>;
#endif //TEST_LENGTHBUFFER_H
